﻿<template>
  <div class="container" style="width:99%;margin-top:-25px;">
    <!-- 查询条件 -->
    <div class="query-box">
      <el-form :model="queryBox" status-icon ref="queryBox">
        <el-row>
          <el-col :span="4">
            <el-form-item label="" prop="studentCode">
              <el-input v-model="queryBox.customerCode" placeholder="客户编号">
                <template slot="prepend">客户编号</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="depart-box">
              <div class="slect-title">去向归属人</div>
              <el-autocomplete
                v-model="userName"
                :fetch-suggestions="querySearchAsync2"
                placeholder="请输入内容"
                clearable
                @select="handleSelect2"
                :change="handleClearable()"
              ></el-autocomplete>
            </div>
          </el-col>
          <el-col :span="4">
            <div class="depart-box">
              <div class="slect-title">来源归属人</div>
              <el-autocomplete
                v-model="userName2"
                :fetch-suggestions="querySearchAsync3"
                placeholder="请输入内容"
                clearable
                @select="handleSelect3"
                :change="handleClearable()"
              ></el-autocomplete>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div class="from-btns">
        <kt-button
          :label="$t('action.reset')"
          perms="true"
          type="primary"
          @click="reset('queryBox')"
        />
        <kt-button
          :label="$t('action.search')"
          perms="true"
          type="primary"
          @click="search"
        />
      </div>
    </div>
    <!--表格内容栏-->
    <Crm-table
      :showBatchDelete="false"
      class="table-width"
      ExchangeStudent
      width="400"
      permsEdit="sys:user:edit"
      permsDelete="sys:user:delete"
      :data="pageResult"
      :columns="columns"
      :loading="loading"
      @findPage="findPage"
    >
    </Crm-table>
  </div>
</template>
<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import distributionPop from "@/components/distributionPop";
import KtButton from "@/views/Core/KtButton";
import CrmTable from "@/views/Core/CrmTable";
import CrmSwitch from "@/views/Core/CrmSwitch";
import CustomerOwnership from "@/components/CustomerOwnership";
import { CodeToText } from "element-china-area-data";
import Cookies from "js-cookie";
import axios from "axios";
export default {
  name: "Student",
  components: {
    CrmTable,
    KtButton
  },
  data() {
    return {
      size: "small",
      // filters: {
      //   name: ""
      // },
      showBatchDelete: false,
      userName: "",
      userName2:'',
      // 查询条件
      queryBox: {
        studentCode: "",
        seatCode: "",
        tqCode: "",
        isGetThrough: "",
        callPeople: "",
        callDate: ""
      },
      callDate:[],
      isGetThrough_options: [
        {
          key: "1",
          value: "接通"
        },
        {
          key: "2",
          value: "未接通"
        }
      ],
      width: 200,
      columns: [
        { type: "index", label: "序号", minWidth: 50 },
        { prop: "customerCode", label: "客户编号", minWidth: 150 },
        { prop: "operation", label: "操作类型", minWidth: 80 },
        { prop: "fromType", label: "来源类型", minWidth: 80 },
        { prop: "from", label: "来源归属人", minWidth: 100 },
        { prop: "toType", label: "去向类型", minWidth: 100 },
        { prop: "to", label: "去向归属人", minWidth: 100 },
        { prop: "createDate", label: "创建时间", minWidth: 100 },
        { prop: "createPeople", label: "创建人", minWidth: 190 },
      ],
      pageRequest: { pageNum: 1, pageSize: 8 },
      pageResult: {
        content: []
      },
      student_progress_options: [],
      student_type_options: [],
      student_data_from_options: [],
      loading: false // 加载标识
    };
  },
  methods: {
    // 重置
    reset(queryBox) {
      this.$refs.queryBox.resetFields();
      this.loading=true
      this.queryBox.customerCode = "";
      this.pageRequest.pageNum = 1;
      this.userName = "";
      this.userName2 = "";
      this.queryBox.to=''
      this.queryBox.from=''
      this.findPage(null);
    },
    // 查询
    search() {
      this.loading = true;
      this.pageRequest.pageNum = 1;
      this.findPage(null);
    },
    // 获取分页数据
    findPage(data) {
      if (data !== null) {
        this.loading = true;
        this.pageRequest = data.pageRequest;
      }
      this.queryBox.pageSize = this.pageRequest.pageSize;
      this.queryBox.pageNum = this.pageRequest.pageNum;
      this.$api.student
        .queryExchangeStu(this.queryBox)
        .then(res => {
          let content = {
            content: res.list,
            totalSize: res.total
          };
          this.pageResult = content;
          // this.pageResult.content = res.studentCallDetailList;
          this.pageResult.content.forEach(item => {
            if (item.studentInfo != null) {
              if (
                item.studentInfo.province == null ||
                item.studentInfo.province == "null"
              ) {
                item.diqu = "";
              } else if (
                (item.studentInfo.province != null &&
                  item.studentInfo.city == null) ||
                (item.studentInfo.province != "null" &&
                  item.studentInfo.city == "null")
              ) {
                item.diqu =
                  CodeToText[item.studentInfo.studentInfo.studentInfo.province];
              } else if (
                (item.studentInfo.province != null &&
                  item.studentInfo.city != null &&
                  item.studentInfo.area == null) ||
                (item.studentInfo.province != "null" &&
                  item.studentInfo.city != "null" &&
                  item.studentInfo.area == "null")
              ) {
                item.diqu =
                  CodeToText[item.studentInfo.province] +
                  CodeToText[item.studentInfo.city];
              } else if (
                (item.studentInfo.province != null &&
                  item.studentInfo.city != null &&
                  item.studentInfo.area != null) ||
                (item.studentInfo.province != "null" &&
                  item.studentInfo.city != "null" &&
                  item.studentInfo.area != "null")
              ) {
                item.diqu =
                  CodeToText[item.studentInfo.province] +
                  CodeToText[item.studentInfo.city] +
                  CodeToText[item.studentInfo.area];
              }
              // 客户进展对应
              this.student_progress_options.forEach(progress_item => {
                if (progress_item.key == item.studentInfo.progress) {
                  item.getprogress = progress_item.value;
                }
              });
              this.student_type_options.forEach(type_item => {
                if (type_item.key == item.studentInfo.type) {
                  item.type = type_item.value;
                }
              });
              this.student_data_from_options.forEach(data_from_item => {
                if (data_from_item.key == item.studentInfo.dataFrom) {
                  item.dataFrom = data_from_item.value;
                }
              });
            }
          });
          this.loading = false;
        })
        .then(data != null ? data.callback : "");
    },
    // 获取来源
    get_student_data_from() {
      this.$api.user
        .optionsList({ codeType: "student_data_from" })
        .then(res => {
          this.student_data_from_options = res.keyValueList;
        });
    },
    get_student_progress() {
      this.$api.user.optionsList({ codeType: "student_progress" }).then(res => {
        this.student_progress_options = res.keyValueList;
      });
    },
    get_student_type() {
      this.$api.user.optionsList({ codeType: "student_type" }).then(res => {
        this.student_type_options = res.keyValueList;
      });
    },
    // 获取用户id
    get_user_id(){ 
      this.$api.user.queryUserById().then(res=>{
        console.log(res)
      })
    },
    // 所属销售搜索
    querySearchAsync2(queryString, callback) {
      let restaurants = [{}];
      let results = "";
      let threeId = JSON.parse(Cookies.get("threeLevelDeptId"));
      if (threeId == null) {
        this.$api.dropdown.users({ userType: 3, deptType: 1 }).then(res => {
          for (let i of res.userInfoList) {
            i.value = i.realName; //将想要展示的数据作为value
          }
          restaurants = res.userInfoList;
          results = queryString
            ? restaurants.filter(this.createFilter2(queryString))
            : restaurants;
          callback(results);
        });
      } else {
        this.$api.dropdown
          .users({ userType: 3, deptType: 1, threeLevelDeptId: this.deptId })
          .then(res => {
            for (let i of res.userInfoList) {
              i.value = i.realName; //将想要展示的数据作为value
            }
            restaurants = res.userInfoList;
            results = queryString
              ? restaurants.filter(this.createFilter2(queryString))
              : restaurants;
            callback(results);
          });
      }
    },
    createFilter2(queryString) {
      return restaurant => {
        return (
          restaurant.realName
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    handleSelect2(item) {
      this.queryBox.to = item.id;
    },
    querySearchAsync3(queryString, callback) {
      let restaurants = [{}];
      let results = "";
      let threeId = JSON.parse(Cookies.get("threeLevelDeptId"));
      if (threeId == null) {
        this.$api.dropdown.users({ userType: 3, deptType: 1 }).then(res => {
          for (let i of res.userInfoList) {
            i.value = i.realName; //将想要展示的数据作为value
          }
          restaurants = res.userInfoList;
          results = queryString
            ? restaurants.filter(this.createFilter3(queryString))
            : restaurants;
          callback(results);
        });
      } else {
        this.$api.dropdown
          .users({ userType: 3, deptType: 1, threeLevelDeptId: this.deptId })
          .then(res => {
            for (let i of res.userInfoList) {
              i.value = i.realName; //将想要展示的数据作为value
            }
            restaurants = res.userInfoList;
            results = queryString
              ? restaurants.filter(this.createFilter3(queryString))
              : restaurants;
            callback(results);
          });
      }
    },
    createFilter3(queryString) {
      return restaurant => {
        return (
          restaurant.realName
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    handleSelect3(item) {
      this.queryBox.from = item.id;
    },
    handleClearable(value) {
      if (this.userName == "") {
        this.queryBox.to = "";
      }
      if (this.userName2 == "") {
        this.queryBox.from = "";
      }
      if (this.userDerpatName == "") {
        this.queryBox.departmentId = "";
      }
      if (this.branchCenterName == "") {
        this.queryBox.providerAgent = "";
      }
    },
  },
  mounted() {
    this.get_student_type();
    this.get_student_progress();
    this.get_student_data_from();
    this.get_user_id()
  }
};
</script>
<style>
@import url("../../assets/queryBox.css");
</style>
